package com.nms.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.nms.model.Email;
import com.nms.model.TakeUser;
import com.nms.model.Type;
import com.nms.model.User;
import com.nms.service.EmailService;
import com.nms.service.TakeService;
import com.nms.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EmailAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private UserService userService;

	private EmailService emailService;

	private TakeService takeService;

	private List<Type> groupList;

	private Email email;

	private List<User> users;

	private User user;

	// Map<String, Object> session =
			// ActionContext.getContext().getSession();
	public EmailService getEmailService() {
		return emailService;
	}

	@Resource(name = "emailService")
	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

	public String sentEmail() {
		
		// user = (User) session.get("user");
		return "sentEmail";
	}

	public String sentTake() {
		// user = (User) session.get("user");
		groupList = takeService.findAllGroup();
		return "sentTake";
	}

	public List<Type> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Type> groupList) {
		this.groupList = groupList;
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}

	@Override
	public String execute() throws Exception {
		// user = (User) session.get("user");
		/* 获得HttpServletRequest对象 */
		HttpServletRequest hsr = ServletActionContext.getRequest();
		/* 获得请求的参数 */
		String act = hsr.getParameter("act");
		int ftemp = Integer.parseInt(email.getFromusername());
		String temp = "";
		switch (ftemp) {
		case 0:
			// temp = user.getUsername();
			break;
		case 1:
			temp = "组管理员";
			break;
		case 2:
			temp = "系统管理员";
			break;
		default:
			break;
		}
		email.setFromusername(temp);
		if (act.equals("saveEmail")) {
			users = userService.findAll();
			for (User u : users) {
				email.setTousername(u.getUsername());
				email.setType(0);
				emailService.save(email);
			}
			return "sentSuccess";
		} else if (act.equals("saveTake")) {
			String totemp = email.getTousername();
			List<TakeUser> tus = takeService.getUserByType(totemp);
			for (TakeUser takeUser : tus) {
				email.setTousername(takeUser.getUsername());
				email.setType(1);
				emailService.save(email);
			}
			return "sentSuccess";
		}
		return "sentError";
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public TakeService getTakeService() {
		return takeService;
	}

	@Resource(name = "takeService")
	public void setTakeService(TakeService takeService) {
		this.takeService = takeService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
